package com.mmall.controller;


import com.mmall.common.ServerResponse;
import com.mmall.pojo.Message;
import com.mmall.pojo.User;
import com.mmall.service.MessageService;
import com.mmall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("message/")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private UserService userService;
    @RequestMapping(value = "create.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse create(Message message,String loginToken){

        ServerResponse response = userService.userIsLogin(loginToken);
        if (!response.isSuccess()) {
            return response;
        }
        User user = (User) response.getData();
        message.setFromUserId(user.getId());
        message.setCreateTime(new Date());
        if(message.getParentMessageId()==null){ //前端未传入父级评论ID，说明这是一个一级留言
            message.setParentMessageId(-999);//设置默认为-999
        }
        return messageService.create(message);
    }
}
